<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 16. Resources</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="smarty.for.programmers.html" title="Part III. Smarty For Programmers">
<link rel="prev" href="caching.custom.html" title="Custom Cache Implementation">
<link rel="next" href="resources.string.html" title="String Template Resources">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Chapter 16. Resources</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="caching.custom.html">Prev</a> </td>
<th width="60%" align="center">Part III. Smarty For Programmers</th>
<td width="20%" align="right"> <a accesskey="n" href="resources.string.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter" title="Chapter 16. Resources">
<div class="titlepage"><div><div><h2 class="title">
<a name="resources"></a>Chapter 16. Resources</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="resources.html#resources.file">File Template Resources</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="resources.html#templates.from.template.dir">Templates from $template_dir</a></span></dt>
<dt><span class="sect2"><a href="resources.html#templates.from.specified.template.dir">Templates from a specific $template_dir</a></span></dt>
<dt><span class="sect2"><a href="resources.html#templates.from.any.dir">Templates from any directory</a></span></dt>
<dt><span class="sect2"><a href="resources.html#templates.windows.filepath">Windows Filepaths</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="resources.string.html">String Template Resources</a></span></dt>
<dt><span class="sect1"><a href="resources.streams.html">Stream Template Resources</a></span></dt>
<dt><span class="sect1"><a href="resources.extends.html">Extends Template Resources</a></span></dt>
<dt><span class="sect1"><a href="resources.custom.html">Custom Template Resources</a></span></dt>
</dl>
</div>
<p>
   The templates may come from a variety of sources. When you
   <a class="link" href="api.display.html" title="display()"><code class="varname">display()</code></a> or
   <a class="link" href="api.fetch.html" title="fetch()"><code class="varname">fetch()</code></a>
   a template, or when you include a template from within another template,
   you supply a resource type, followed by the appropriate path and template
   name. If a resource is not explicitly given, the value of 
   <a class="link" href="variable.default.resource.type.html" title="$default_resource_type"><em class="parameter"><code>$default_resource_type</code></em></a> 
   (default: "file") is assumed.
  </p>
<div class="sect1" title="File Template Resources">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="resources.file"></a>File Template Resources</h2></div></div></div>
<p>
  Smarty ships with a built-in template resource for the filesystem. The <code class="literal">file:</code> is the default resource. 
  The resource key <code class="literal">file:</code> must only be specified, if the 
  <a class="link" href="variable.default.resource.type.html" title="$default_resource_type"><em class="parameter"><code>$default_resource_type</code></em></a> has been changed.
 </p>
<p>
  If the file resource cannot find the requested template, the 
  <a class="link" href="variable.default.template.handler.func.html" title="$default_template_handler_func"><em class="parameter"><code>$default_template_handler_func</code></em></a>
  is invoked.	
 </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
   As of Smarty 3.1 the file resource no longer walks through the 
   <a class="ulink" href="http://php.net/ini.core.php#ini.include-path" target="_top">include_path</a> unless
   <a class="link" href="variable.use.include.path.html" title="$use_include_path"><em class="parameter"><code>$use_include_path</code></em> is activated</a>
  </p>
</div>
<div class="sect2" title="Templates from $template_dir">
<div class="titlepage"><div><div><h3 class="title">
<a name="templates.from.template.dir"></a>Templates from $template_dir</h3></div></div></div>
<p>
   The file resource pulls templates source files from the directories specified in 
   <a class="link" href="variable.template.dir.html" title="$template_dir"><em class="parameter"><code>$template_dir</code></em></a>. 
   The list of directories is traversed in the order they appear in the array. 
   The first template found is the one to process.
  </p>
<div class="example">
<a name="id455179"></a><p class="title"><b>Example 16.1. Using templates from the $template_dir</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;display('index.tpl');
$smarty-&gt;display('file:index.tpl'); // same as above
?&gt;

   </pre>
<p>From within a Smarty template</p>
<pre class="programlisting">

{include file='index.tpl'}
{include file='file:index.tpl'} {* same as above *}

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Templates from a specific $template_dir">
<div class="titlepage"><div><div><h3 class="title">
<a name="templates.from.specified.template.dir"></a>Templates from a specific $template_dir</h3></div></div></div>
<p>
   Smarty 3.1 introduced the bracket-syntax for specifying an element from 
   <a class="link" href="variable.template.dir.html" title="$template_dir"><em class="parameter"><code>$template_dir</code></em></a>.
   This allows websites employing multiple sets of templates better control over
   which template to acces.
  </p>
<p>
   The bracket-syntax can be used from anywhere you can specify the 
   <code class="literal">file:</code> resource type.
  </p>
<div class="example">
<a name="id455241"></a><p class="title"><b>Example 16.2. Specifying the $template_dir to use</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php

// setup template directories
$smarty-&gt;setTemplateDir(array(
    './templates',            // element: 0, index: 0
    './templates_2',          // element: 1, index: 1
    '10' =&gt; 'templates_10',   // element: 2, index: '10'
    'foo' =&gt; 'templates_foo', // element: 3, index: 'foo'
));

/*
  assume the template structure
  ./templates/foo.tpl
  ./templates_2/foo.tpl
  ./templates_2/bar.tpl
  ./templates_10/foo.tpl
  ./templates_10/bar.tpl
  ./templates_foo/foo.tpl
*/

// regular access
$smarty-&gt;display('file:foo.tpl'); 
// will load ./templates/foo.tpl

// using numeric index
$smarty-&gt;display('file:[1]foo.tpl'); 
// will load ./templates_2/foo.tpl

// using numeric string index
$smarty-&gt;display('file:[10]foo.tpl'); 
// will load ./templates_10/foo.tpl

// using string index
$smarty-&gt;display('file:[foo]foo.tpl'); 
// will load ./templates_foo/foo.tpl

// using "unknown" numeric index (using element number)
$smarty-&gt;display('file:[2]foo.tpl'); 
// will load ./templates_10/foo.tpl

?&gt;

   </pre>
<p>From within a Smarty template</p>
<pre class="programlisting">

{include file="file:foo.tpl"}
{* will load ./templates/foo.tpl *}

{include file="file:[1]foo.tpl"}
{* will load ./templates_2/foo.tpl *}

{include file="file:[foo]foo.tpl"}
{* will load ./templates_foo/foo.tpl *}

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Templates from any directory">
<div class="titlepage"><div><div><h3 class="title">
<a name="templates.from.any.dir"></a>Templates from any directory</h3></div></div></div>
<p>
   Templates outside of the 
   <a class="link" href="variable.template.dir.html" title="$template_dir"><em class="parameter"><code>$template_dir</code></em></a>
   require the <code class="literal">file:</code> template resource type, followed by
   the absolute path to the template (with leading slash.)
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    With <a class="link" href="advanced.features.html#advanced.features.security" title="Security"><code class="varname">Security</code></a> enabled, 
    access to templates outside of the 
    <a class="link" href="variable.template.dir.html" title="$template_dir"><em class="parameter"><code>$template_dir</code></em></a> 
    is not allowed unless you list those directories in <em class="parameter"><code>$secure_dir</code></em>.
   </p>
</div>
<div class="example">
<a name="id455348"></a><p class="title"><b>Example 16.3. Using templates from any directory</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;display('file:/export/templates/index.tpl');
$smarty-&gt;display('file:/path/to/my/templates/menu.tpl');
?&gt;

   </pre>
<p>
    And from within a Smarty template:
   </p>
<pre class="programlisting">

{include file='file:/usr/local/share/templates/navigation.tpl'}

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Windows Filepaths">
<div class="titlepage"><div><div><h3 class="title">
<a name="templates.windows.filepath"></a>Windows Filepaths</h3></div></div></div>
<p>
   If you are using a Windows machine, filepaths usually include a
   drive letter (C:) at the beginning of the pathname. Be sure to use
   <code class="literal">file:</code> in the path to avoid namespace conflicts and
   get the desired results.
  </p>
<div class="example">
<a name="id455398"></a><p class="title"><b>Example 16.4. Using templates from windows file paths</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;display('file:C:/export/templates/index.tpl');
$smarty-&gt;display('file:F:/path/to/my/templates/menu.tpl');
?&gt;

  </pre>
<p>
   And from within Smarty template:
  </p>
<pre class="programlisting">

{include file='file:D:/usr/local/share/templates/navigation.tpl'}

</pre>
</div>
</div>
<br class="example-break">
</div>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="caching.custom.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="smarty.for.programmers.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="resources.string.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Custom Cache Implementation </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> String Template Resources</td>
</tr>
</table>
</div>
</body>
</html>
